package com.herui.ota.service.iml;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.herui.ota.dao.mysql.DeviceOtaTreeMapper;
import com.herui.ota.dao.mysql.DeviceTreeMapper;
import com.herui.ota.entity.BaseTree;
import com.herui.ota.entity.DeviceOtaTree;
import com.herui.ota.entity.DeviceTree;
import com.herui.ota.pojo.dto.TreeManagerWrap;
import com.herui.ota.service.DeviceOtaTreeService;
import com.herui.ota.service.DeviceTreeService;
import com.mlnx.common.utils.MyLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * author name: amanda.shan
 * create time: 2024-03-30 11:56:21
 */
public class DeviceTreeServiceIml extends BaseService<DeviceTreeMapper, DeviceTree> implements DeviceTreeService {

    MyLog log = MyLog.getLog(getClass());

    private TreeManagerWrap treeManagerWrap;

    @Autowired
    @Override
    public void setBaseMapper(DeviceTreeMapper baseMapper) {
        super.setBaseMapper(baseMapper);
    }

//    @PostConstruct
    public void init() {
        List<DeviceTree> list = list();
        List<BaseTree> mockData = new ArrayList<>();
        mockData.add(new DeviceOtaTree(0, -1, "root", 0));
        mockData.addAll(list);

        BaseTree allDeviceOtaTree = TreeManagerWrap.buildTree(mockData);
        TreeManagerWrap.printTree(allDeviceOtaTree);

        treeManagerWrap = new TreeManagerWrap(allDeviceOtaTree);

        log.info("设备数信息:{}", JSON.toJSON(allDeviceOtaTree));
    }

    @Override
    public List<BaseTree> getDeviceOtaTrees() {
        return treeManagerWrap.getDeviceOtaTree().getBaseTrees();
    }

    @Transactional
    public synchronized void saveTree(String treeInfo) {

        List<DeviceTree> tempDeviceOtaTrees = new LinkedList<>();

        String[] nodes = treeInfo.split("-");
        // 返回两个参数 那一层不匹配（层数是从0开始）   不匹配的前一个参数id
        int[] missingLevel = treeManagerWrap.findMissingLevel(treeInfo);
        if (missingLevel[0] != -1) {
            int parentId = missingLevel[1];
            for (int i = missingLevel[0]; i < nodes.length; i++) {
                DeviceTree deviceTree = new DeviceTree();
                deviceTree.setLevel(i + 1);
                deviceTree.setParentId(parentId);
                deviceTree.setNodeData(nodes[i]);
                save(deviceTree);
                parentId = deviceTree.getId();
                tempDeviceOtaTrees.add(deviceTree);
            }
        }

        for (DeviceTree deviceOtaTree : tempDeviceOtaTrees) {
            treeManagerWrap.insertNode(deviceOtaTree);
        }
//        TreeManagerWrap.printTree(treeManagerWrap.getDeviceOtaTree());
    }

    @Transactional
    @Override
    public synchronized void resetAll(List<String> treeInfos) {
        remove(new QueryWrapper<>());

        List<BaseTree> mockData = new ArrayList<>();
        mockData.add(new DeviceOtaTree(0, -1, "root", 0));
        BaseTree allDeviceOtaTree = TreeManagerWrap.buildTree(mockData);
        treeManagerWrap = new TreeManagerWrap(allDeviceOtaTree);
        for (String treeInfo: treeInfos) {
            saveTree(treeInfo);
        }

        log.info("设备数信息:{}", JSON.toJSON(allDeviceOtaTree));
    }


}

